/*
 * Package il.ac.biu.cs.grossmm.impl.keys
 * File AttributeValue.java
 * Created on Jul 20, 2006
 *
 */
package il.ac.biu.cs.grossmm.api.keys;

import java.util.NoSuchElementException;

/**
 * A default implementation for a simple key. Simple key is a key
 * which does not contain subkeys.
 */
public class SimpleKey implements Key {
    private final Object value;
    
    public SimpleKey(Object value) {
        this.value = value;
    }

    public KeyEntry entry(int i) {
        throw new NoSuchElementException();
    }

    public Key subkey(Attribute a) {
        return null;
    }

    public Object value() {
        return value;
    }

    public Object value(Attribute a) {
        return null;
    }
    
    public int size() {
        return 0;
    }

    @Override
    public int hashCode() {
        return value.hashCode();
    }
    
    @Override
    public boolean equals(Object obj) {
        if( obj instanceof SimpleKey ) {
            if( obj == this )
                return true;
            
            return ((SimpleKey) obj).value.equals(value);
        }

        if( obj instanceof Key ) {
            Key key = (Key) obj;
            
            if( key.size() != 0 )
                return false;
            
            if( value == null )
                return key.value() == null;
            else
                return value.equals(key.value());
        }
        
        return false;
    }
    
    @Override
    public String toString() {
        if( value == null )
            return "null";
        return value.toString();
    }
}
